<?php

    class StringUtil
    {

        /**
         *
         * @param type $text
         * @param type $limit
         * @return type 
         */
        public static function limitCharacter($text, $limit, $endStr = '...')
        {
            $length = strlen($text);
            if ($length < $limit)
            {
                return $text;
            }
            else
            {
                return substr($text, 0, $limit) . $endStr;
            }
        }

        public static function removeSpecialCharacter($str)
        {
            return preg_replace('/[^a-zA-Z0-9]/s', '', $str);
        }

        public static function generateRandomString($length = 10)
        {
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = '';
            for ($i = 0; $i < $length; $i++)
            {
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
            }
            return $randomString;
        }

        

        public static function replaceSingleWord($search,$replace,$needle){
            $pos1 = stripos($needle,$search);
            $length = strlen($search);
            if($pos1!==false){
                $start= substr($needle,0,$pos1);
                $mid = $replace;
                $end = substr($needle,$pos1+$length);
                return $start.$mid.$end;
            }
            return $needle;
        }

        

    }

?>
